package com.itheima.tliasmanager.controller;

import com.itheima.tliasmanager.pojo.Vo.LoginVo;
import com.itheima.tliasmanager.pojo.domain.Emp;
import com.itheima.tliasmanager.pojo.Vo.Result;
import com.itheima.tliasmanager.service.EmpService;
import com.itheima.tliasmanager.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("C----->员工登录：{}", emp);

        Emp empResult = empService.chaeckLogin(emp);

        LoginVo loginVo = new LoginVo();

        //spring框架提供,实体对象属性拷贝:将一个对象的属性拷贝到另一个对象相同的属性中
        BeanUtils.copyProperties(empResult, loginVo);
        Map claims = new HashMap();
        claims.put("id", empResult.getId());
        claims.put("username", empResult.getUsername());
        claims.put("name", empResult.getName());

        String token = JwtUtils.generateJwt(claims);

        loginVo.setToken(token);
        return Result.success(loginVo);
    }
}
